//服务器地址
let config = require('../config/config.js')

const host = config.upload_url

/**
 * 封装wx.request请求
 * @param {String} url 请求接口地址
 * @param {Object} data 要上传文件资源的路径 (本地路径)
 * @param {String} key 文件对应的 key
 */
function file(url, data, key, header={}) {
	return new Promise((resolve, reject) => {
		if (uni.getStorageSync('dwu_token')) {
			header['token'] = uni.getStorageSync('dwu_token')
		}
		uni.uploadFile({
			url: host + url,
			filePath: data,
			header,
			name: key,
			formData: {
				method: 'POST', //请求方式
			},
			success(res) {
				if (res.statusCode == 200) {
					resolve(res.data)
				} else {
					reject(res.data)
				}
				//do something
			},
			fail(err) {
				reject(err)
			}
		})
	})
}

// 导出http方法
module.exports = {
	file
}